% Settings to load draws, save files 
% ====================================
cuc=cd; 


disp('================================================================='); 
disp('Begin Plotting prior and posterior kernel estimates for the draws'); 
disp(' '); 

opath=[cuc,'\',auth,'\',spec,'\',subf]; 
mkdir(opath,['posterior_',loadstr]); 
spath=[opath,'\posterior_',loadstr];


fpraid('Loading Draws'); 
cd(opath); 
load(['settings',loadstr]); 
draws=load(['mdraws',loadstr]);
[ch1,ch2]=size(draws); 
cd(cuc); 
fpraid('End Loading Draws'); 
fullp(con)=calpr; 
disp( [' Calibrated Values are : ',num2str( fullp' ) ] ); 
ncof=length(indp); 

% Check draws dimensions 
% ------------------------
[ndr,nce]=size(draws);

% =============================
% Trim the draws, if excessive 
% =============================
if isempty(sret)==1 | sret==1 
    disp('No trimming') 
else 
    if sret > 1 | sret < 0; 
        error('Share must be between zero and one'); 
    end; 
    temp=round(sret*ndr);
    draws=draws(temp+1:end,:); 
    disp(['Retained ',num2str(sret),' of the sample'] ); 
    ndr=size(draws,1);
end 
disp(['Sample: ',num2str(ndr),' draws']); 

if nce ~= ncof; 
    error(' Mistmatch dimension draws and non-calibrated parameters'); 
end; 

dist=char(dist(indp));
alphap=alphap(indp); 
betap=betap(indp); 
cofname=cofname(indp); 


% Do normal Kernel First 
set(0,'DefaultAxesFontSize',6); 
nturn=1; 

nstr={'NK';'EK'};


% e1=ch_field(into,'m'); 
% if e1==1 
%     m=into.m ;
% end
% e2=ch_field(into,'n'); 
% if e2==1 
%     n=into.n ;
% end
% e=ch_field(into,'def',1); 
% def=into.def; 
% 
% if def == 1
%     if ( ( e1==0 ) | ( (e1+e2)== 0 ) | ( m*n~=nce ) )
%         n=2 ;
%         m=ceil(nce/n) ; 
%     end
% elseif def == 2 
%     if  ( ( e2 == 0 ) | ( (e1+e2)== 0 ) | ( m*n~=nce ) )  
%         m=3 ; 
%         n=ceil(nce/m) ; 
%     end
% end 
gpp=m*n; 
npag=ceil(nce/gpp); 


% ===============================================
% Begin the prior and posterior for each Kernel %
% ===============================================
cser=0; nn=1; 

for nturn=1:1; 
    ipag=0;
    jj=0;
    for ipag=1:npag;
        iser=1; 
        pagstr=num2str(ipag); 
        
        for iser=1:gpp; 
            cser=cser+1; 
            
            jj=jj+1; 
            cname=cofname{jj};
            disp(['Coefficient : ',cname] ); 
            % =================================================
            %                   Prior Draws                   %
            % =================================================
            prdraws=genrand(dist(jj),alphap(jj),betap(jj),ndr); 
            
            if ( strcmp( dist(jj),'I') ==1 & alphap(jj) < 2.1 ) |... 
                ( strcmp( dist(jj),'W') ==1 & alphap(jj) < 4.1 )
                np=400; 
                pvec=0.025:0.025:0.9;
                pvec=[0.01 pvec 0.99]; 
                clim=1; 
            else    
                np=100; 
                pvec=[0.01 0.1 0.9 0.99]; 
                clim=0; 
            end
           
            if nturn==1
            
                % =============================================
                %                  NTURN == 1         
                % Compute Normal Kernel for prior and posterior
                % =============================================
                [h,gpr,fpr]=normkern(prdraws,0,3,np);   
                [h,gpo,fpo]=normkern(draws(:,jj),0,3,np);  
                
            else
                % =============================================
                %                  NTURN == 2         
                % Compute E Kernel for prior and posterior
                % =============================================
                [h,fpr,gpr]=pltdens(prdraws,[],[],2); 
                [h,fpo,gpo]=pltdens(draws(:,jj),[],[],2);
            end
            
            % ======================================================
            %     For the case of very diffuse priors (clim==1), 
            %  trim the domain  to allow posterior to be discernible 
            % ======================================================
            prpr=myprctile(gpr,pvec); 
            prpo= myprctile(gpo,pvec);        
            
            if clim==1
               crej=1; 
                hh=1; 
                for hh=1:37; 
                    if prpr(end-hh) > prpo(end) 
                        crej=crej+1; 
                    else 
                        break
                    end
                end
                lb=min( prpo(1), prpr(1) ) ;
                ub=prpr(end-crej+1);
            else 
                lb=min( prpr(1), prpo(1) ); 
                ub=max( prpr(end), prpo(end) ); 
            end 
            % ===========================================================
            
            if iser==1 
                figure; 
            end
            subplot(m,n,iser); 
            hold on; 
            plot(gpr,fpr,'-.r','LineWidth',2); 
            plot(gpo,fpo,'-b','LineWidth',1.5);
            XLim([lb ub]);
            hold off; 
            clear gpo gpr fpr fpo; 
            title(cname,'FontSize',7,'FontWeight','Bold') ;  
            lhand=legend('PR','PO',0);
            set(lhand,'FontSize',5,'FontWeight','Bold');
            
            
            if isempty( brpoint) == 0; 
                brpoint=sort(brpoint); 
                
                if brpoint(nn)== cser; 
                    nn=nn+1; 
                    
                    break
                end
            end 
            
            
            if ipag==npag & jj==nce; 
                break
            end 
        end % Per Series
        cd(spath); 
        saveas(gcf,['g_post_',pagstr],'fig'); 
        cd(cuc);
        
    end % Per page 
end % Nturn

    